package com.woniu.permissions.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.exception.permissions.PermissionsException;
import com.woniu.exception.permissions.PermissionsExceptionCode;
import com.woniu.permissions.model.Role;
import com.woniu.permissions.mapper.RoleMapper;
import com.woniu.permissions.param.UpdateRoleNameParam;
import com.woniu.permissions.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    /**
     * 新增角色
     * @param roleName 角色名
     * @return 输出新增成功的角色对象
     */
    @Override
    public Role insertRole(String roleName) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",roleName);
        Role role = roleMapper.selectOne(wrapper);
        if (role!=null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_ROLE);
        }
        role.setRoleName(roleName);
        roleMapper.insert(role);
        return role;
    }

    /**
     * 根据id修改角色名
     * @param param 角色id
     * @return 输出角色对象
     */
    @Override
    public Role updateRole(UpdateRoleNameParam param) {
        Role role = roleMapper.selectById(param.getId());
        if (role==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NOT_ROLE);
        }
        role.setRoleName(param.getRoleName());
        roleMapper.updateById(role);
        return role;
    }

    /**
     * 根据id删除角色
     * @param id 角色id
     * @return 删除的角色对象
     */
    @Override
    public Role delRole(String id) {
        return null;
    }
}
